package org.example.controller;

import org.example.common.ResponseEntity;
import org.example.dto.SaveTestResultDTO;
import org.example.entity.ReactionTestRecord;
import org.example.service.ReactionTestService;
import org.example.utils.UserContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletRequest;

import java.util.List;

@RestController
@RequestMapping("/api/reaction")
public class ReactionTestController {

    @Autowired
    private ReactionTestService reactionTestService;
    
    @Autowired
    private HttpServletRequest request;

    /**
     * 保存测试结果
     */
    @PostMapping("/save")
    public ResponseEntity saveTestResult(@RequestBody SaveTestResultDTO dto) {
        try {
            Long userId = UserContextUtil.getCurrentUserIdOrThrow(request);
            Long recordId = reactionTestService.saveTestResult(dto, userId);
            return ResponseEntity.ok("测试结果保存成功，记录ID: " + recordId);
        } catch (Exception e) {
            return ResponseEntity.error("保存测试结果失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户历史记录
     */
    @GetMapping("/history")
    public ResponseEntity getUserHistory(@RequestParam(defaultValue = "1") Integer page, 
                                        @RequestParam(defaultValue = "10") Integer size) {
        try {
            Long userId = UserContextUtil.getCurrentUserIdOrThrow(request);
            List<ReactionTestRecord> records = reactionTestService.getUserHistory(userId, page, size);
            return ResponseEntity.ok(records);
        } catch (Exception e) {
            return ResponseEntity.error("获取历史记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户最佳成绩
     */
    @GetMapping("/best")
    public ResponseEntity getUserBest() {
        try {
            Long userId = UserContextUtil.getCurrentUserIdOrThrow(request);
            ReactionTestRecord bestRecord = reactionTestService.getUserBestResult(userId);
            return ResponseEntity.ok(bestRecord);
        } catch (Exception e) {
            return ResponseEntity.error("获取最佳成绩失败: " + e.getMessage());
        }
    }

    /**
     * 获取排行榜
     */
    @GetMapping("/ranking")
    public ResponseEntity getRanking(@RequestParam(defaultValue = "20") Integer limit) {
        try {
            List<ReactionTestRecord> ranking = reactionTestService.getRanking(limit);
            return ResponseEntity.ok(ranking);
        } catch (Exception e) {
            return ResponseEntity.error("获取排行榜失败: " + e.getMessage());
        }
    }

    /**
     * 获取测试记录详情
     */
    @GetMapping("/detail/{recordId}")
    public ResponseEntity getTestDetail(@PathVariable Long recordId) {
        try {
            Long userId = UserContextUtil.getCurrentUserIdOrThrow(request);
            ReactionTestRecord record = reactionTestService.getTestDetail(recordId, userId);
            return ResponseEntity.ok(record);
        } catch (Exception e) {
            return ResponseEntity.error("获取测试详情失败: " + e.getMessage());
        }
    }
}